package lynch.core 
{
	import Elegant.ELGManager;
	import Elegant.utils.core.Singleton;
	import Elegant.utils.InitObject;
	import flash.display.Stage;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import lynch.manager.UIManager;
	import lynch.map.MiniMap;
	import lynch.map.WorldMap;
	import lynch.net.Client;
	import lynch.net.loading.interfaces.IProgressBar;
	import lynch.net.loading.ResourceLoader;
	import lynch.net.Server;
	import lynch.net.SocketConnector;
	/**
	 * games entry
	 * @author 小痛
	 */
	public class Core 
	{
		public static var map:WorldMap;
		public static var miniMap:MiniMap;
		public static var client:Client;
		public static var server:Server;
		public static var stage:Stage;
		public static var config:Config;
		
		private static var onComplete:Function;
		/**
		 * @throws flash.errors.IllegalOperationError	no need to create an instance of Core
		 */
		public function Core()
		{
			throw new IllegalOperationError("no need to create any instance of Core");
		}
		
		/**
		 * 
		 * @param	stage
		 * @param	onComplete	唯一参数 Array
		 * @param	objectVars	<li><strong> alertConstructor : Class || String </strong>弹出窗口类,Class 或 String</li>
		 * 						<li><strong> configXMLPath : String </strong></li>
		 * 						<li><strong> progressBar : IProgressBar </strong></li>
		 */
		public static function initialize(stage:Stage, onComplete:Function, objectVars:Object = null):void
		{
			Core.onComplete = onComplete;
			
			ResourceLoader.loadTexts(Vector.<String>([InitObject.getString(objectVars, "configXMLPath", "resource/config/config.xml")]), onConfigComplete, { params:[objectVars] } );
			ResourceLoader.registerProgressBar(IProgressBar(InitObject.getObject(objectVars, "progressBar")));
			
			//------------Clear stage-------------------------
			Core.stage = stage;
			stage.align = "TL";
			stage.scaleMode = "noScale";
			//stage.removeChildAt(0);
		}
		
		private static function onConfigComplete(data:Array, objectVars:Object = null):void 
		{
			//------------GlobeCore.config.initialize---------------
			Core.config = new Config;
			Core.config.configXML = XML(data[0]);
			
			var gameConfig:XML = Core.config.configXML.gameConfig[0];
			Core.config.SERVER_IP = gameConfig.net.@ip;
			Core.config.SERVER_PORT = gameConfig.net.@port;
			Core.config.tileWidth = gameConfig.map.@tileWidth;
			Core.config.tileHeight = gameConfig.map.@tileHeight;
			onResize(null);
			
			stage.removeChildAt(0);
			stage.addEventListener(Event.RESIZE, onResize, false, 1);
			//------------net initialize---------------
			server = new Server();
			client = new Client();
			
			var connector:SocketConnector = SocketConnector.arcane::initialize();
			connector.receiveder = server.arcane::protocol;
			connector.connect(Core.config.SERVER_IP, Core.config.SERVER_PORT);
			
			//------------Singleton initialize---------
			var ui:UIManager = Singleton.create(UIManager);
			stage.addChild(UIManager.rootSprite);
			
			ELGManager.init(UIManager.rootSprite, InitObject.getObject(objectVars, "alertConstructor"));
			
			//------------map initialize---------------
			map = new WorldMap();
			miniMap = map.getMiniMap();
			ui.buttomLayer.addChild(map);
			
			onComplete();
			onComplete = null;
		}
		
		private static function onResize(e:Event):void 
		{
			var w:int = Math.min(Math.max(stage.stageWidth, Core.config.configXML.gameConfig.@minWidth), Core.config.configXML.gameConfig.@maxWidth);
			var h:int = Math.min(Math.max(stage.stageHeight, Core.config.configXML.gameConfig.@minHeight), Core.config.configXML.gameConfig.@maxHeight);
			Core.config.arcane::stageWidth = w;
			Core.config.arcane::stageHeight = h;
			
			if (map)
				map.flashTo(map.x, map.y);
		}
	}
}